﻿using Dapper;
using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-08-23 17:31
    /// 描 述：命名规则，与对象类型和属性都有关联
    /// </summary>
    public class ec_objecttypenamingconvService : RepositoryFactory
    {
        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_objecttypenamingconvEntity> GetList(string queryJson)
        {
            try
            {
                //参考写法
                var queryParam = queryJson.ToJObject();
                // 虚拟参数
                var dp = new DynamicParameters(new { });
                //dp.Add("startTime", queryParam["StartTime"].ToDate(), DbType.DateTime);


                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(" t.* ");
                if (queryParam["ProjectId"].IsEmpty())
                {
                    strSql.Append(" FROM ec_objecttypenamingconv t where 1=1");
                }
                else
                {
                    var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(queryParam["ProjectId"].ToString());

                    strSql.Append(" FROM ec_objecttypenamingconv_" + ProjectEntity.ProjectIndex + " t where 1=1");
                }
                if (!queryParam["ObjectTypeID"].IsEmpty())
                {
                    strSql.Append(" and ObjectTypeID='" + queryParam["ObjectTypeID"].ToString() + "'");
                }
                strSql.Append("order by Segmentno");
                return this.BaseRepository().FindList<ec_objecttypenamingconvEntity>(strSql.ToString());
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取列表分页数据
        /// <param name="pagination">分页参数</param>
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_objecttypenamingconvEntity> GetPageList(Pagination pagination, string queryJson)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(" t.* ");
                strSql.Append(" FROM ec_objecttypenamingconv t ");
                return this.BaseRepository().FindList<ec_objecttypenamingconvEntity>(strSql.ToString(), pagination);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_objecttypenamingconvEntity GetEntity(string keyValue, string ProjectId)
        {
            try
            {

                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    return this.BaseRepository().FindEntity<ec_objecttypenamingconvEntity>(keyValue);
                }
                else
                {
                    var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                    var strSql = new StringBuilder();
                    strSql.Append($@"SELECT * FROM ec_objecttypenamingconv_{ProjectEntity.ProjectIndex} WHERE ObjectTypeNVID = @ObjectTypeNVID ");
                    List<ec_objecttypenamingconvEntity> list = this.BaseRepository().FindList<ec_objecttypenamingconvEntity>(strSql.ToString(), new { ObjectTypeNVID = keyValue }).ToList();
                    return list.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue)
        {
            try
            {
                this.BaseRepository().Delete<ec_objecttypenamingconvEntity>(t => t.ObjectTypeNVID == keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ec_objecttypenamingconvEntity entity, string ProjectId)
        {
            try
            {
                //根据属性id去找属性的名字
                var PropertyEntity = new ec_propertyService().GetEntity(entity.ObjectTypePID, ProjectId);
                if (PropertyEntity != null)
                {
                    entity.ObjectTypePName = PropertyEntity.PropertyNameEN;
                }

                if (!string.IsNullOrEmpty(keyValue))
                {
                    entity.Modify(keyValue);

                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        this.BaseRepository().Update(entity);
                    }
                    else
                    {
                        var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                        this.BaseRepository().UpdateByNoMap(entity, $"ec_objecttypenamingconv_{ProjectEntity.ProjectIndex}", "ObjectTypeNVID");
                    }

                }
                else
                {
                    entity.Create();

                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        this.BaseRepository().Insert(entity);
                    }
                    else
                    {
                        var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                        this.BaseRepository().InsertByNoMap(entity, $"ec_objecttypenamingconv_{ProjectEntity.ProjectIndex}");
                    }
                }


            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
